<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="设备编号" prop="deviceNo">
        <el-input v-model="queryParams.deviceNo" placeholder="请输入设备编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备状态" prop="deviceState">
        <el-select v-model="queryParams.deviceState" placeholder="请选择设备状态" clearable>
          <el-option v-for="dict in dict.type.device_state" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备负责人" prop="deviceManager">
        <el-select v-model="queryParams.deviceManager" placeholder="请选择设备负责人" clearable>
          <el-option v-for="dict in dict.type.lyner_emp" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备登记时间">
        <el-date-picker v-model="daterangeRegisterTime" style="width: 330px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="设备地图地址" prop="deviceAddress">
        <el-input v-model="queryParams.deviceAddress" placeholder="请输入设备地图地址" clearable style="width: 325px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:device:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:device:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:device:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:device:export']">导出</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="ce"
         >测试</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-warning" size="mini" @click="handleSmoke"
        v-hasRole="['admin']" >点击触发烟雾报警器</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" width="85" />
      <el-table-column label="设备编号" align="center" prop="deviceNo" width="85" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="设备状态" align="center" prop="deviceState" width="85">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_state" :value="scope.row.deviceState" />
        </template>
      </el-table-column>
      <el-table-column label="设备负责人" align="center" prop="deviceManager" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lyner_emp" :value="scope.row.deviceManager" />
        </template>
      </el-table-column>
      <el-table-column label="设备登记时间" align="center" prop="registerTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备地图地址" align="center" prop="deviceAddress" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:device:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:device:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-setting" @click="getLimte(scope.row)">设置设备限制</el-button>
          <el-button size="mini" type="text" icon="el-icon-s-order" @click="getDetail(scope.row)">设备详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="设备编号" prop="deviceNo">
          <el-input v-model="form.deviceNo" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备状态" prop="deviceState">
          <el-select v-model="form.deviceState" placeholder="请选择设备状态">
            <el-option v-for="dict in dict.type.device_state" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备负责人" prop="deviceManager">
          <el-select v-model="form.deviceManager" placeholder="请选择设备负责人">
            <el-option v-for="dict in dict.type.lyner_emp" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备登记时间" prop="registerTime">
          <el-date-picker clearable v-model="form.registerTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择设备登记时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="设备地图地址" prop="deviceAddress">
          <el-input v-model="form.deviceAddress" placeholder="请输入设备地图地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 设置限制对话框 -->
    <el-dialog :title="title" :visible.sync="op" width="1000px" append-to-body>
      <el-table :data="tableData" style="width: 100%">
        <!-- <el-table-column
        prop="id"
        label="序号"
        > -->
        <!-- </el-table-column> -->

        <el-table-column prop="deviceNo" label="设备名称">
        </el-table-column>
        <el-table-column prop="limitName" label="检查项">
        </el-table-column>
        <el-table-column prop="limitUpper" label="报警上限">
        </el-table-column>
        <el-table-column prop="limitLower" label="报警下限">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="updateLimit(scope.row)" type="text" size="small">修改</el-button>
            <!-- <el-button type="text" size="small">编辑</el-button> -->
          </template>
          <!-- <el-button @click="updateLimit" type="text" size="small">修改</el-button> -->

        </el-table-column>

      </el-table>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="limit" width="500px" append-to-body>
      <el-form ref="limitList" :model="limitList" :rules="rules" label-width="80px">

        <el-form-item label="设备编号" prop="deviceNo">
          <el-input v-model="limitList.deviceNo" placeholder="请输入报警上限" :disabled="true" />
        </el-form-item>
        <el-form-item label="检查项" prop="limitName">
          <el-input v-model="limitList.limitName" placeholder="请输入报警上限" :disabled="true" />
        </el-form-item>
        <el-form-item label="报警上限" prop="limitUpper">
          <el-input v-model="limitList.limitUpper" placeholder="请输入报警上限" :required="true" />
        </el-form-item>
        <el-form-item label="报警下限" prop="limitLower">
          <el-input v-model="limitList.limitLower" placeholder="请输入报警下限" :required="true" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="setLimit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getSmokeAPI, listDevice, getDevice, delDevice, addDevice, updateDevice, setLimitAPI, getLimitAPI, getLimitidAPI ,getListAPI} from "@/api/system/device";



export default {
  name: "Device",
  dicts: ['lyner_emp', 'device_state'],
  data() {
    return {
      smoke:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      op: false,
      limit: false,
      // 设备地图地址时间范围
      daterangeRegisterTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceNo: null,
        deviceName: null,
        deviceState: null,
        deviceManager: null,
        registerTime: null,
        deviceAddress: null
      },
      // 表单参数
      form: {},
      tableData: [],
      limitList: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeRegisterTime && '' != this.daterangeRegisterTime) {
        this.queryParams.params["beginRegisterTime"] = this.daterangeRegisterTime[0];
        this.queryParams.params["endRegisterTime"] = this.daterangeRegisterTime[1];
      }
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.op = false;
      this.limit = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deviceNo: null,
        deviceName: null,
        deviceState: null,
        deviceManager: null,
        registerTime: null,
        deviceAddress: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRegisterTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDevice(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {

        if (valid) {
          if (this.form.id != null) {
            updateDevice(this.form).then(response => {
              // console.log(response);
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备编号为"' + ids + '"的数据项？').then(function () {
        return delDevice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/device/export', {
        ...this.queryParams
      }, `device_${new Date().getTime()}.xlsx`)
    },
    // 查询设置限制的弹框
    getLimte(row) {
      this.reset();
      this.op = true;
      const id = row.id || this.ids;
      // console.log(id);
      const No = row.deviceNo
      getLimitAPI(No).then(response => {
        // console.log(response);
        this.tableData = response.data;
        this.title = "设置设备限制";
      });
    },
    // 跳转设备详情
    getDetail(row) {
      this.reset();
      const id = row.id
      // console.log(id);
      getListAPI(id).then(response => {
      //  console.log(response);
      var addres=response.data[0].deviceRouter
      console.log(this);
        this.$router.push(addres);
      });
    },
    updateLimit(row) {
      const id = row.id;
      getLimitidAPI(id).then(response => {
        // console.log(response);
        this.limitList = response.data[0];

      });

      this.limit = true
    },
    setLimit() {
      this.$refs["limitList"].validate(valid => {

        if (valid) {
          if (this.limitList.id != null) {
            setLimitAPI(this.limitList).then(response => {
              //  console.log(response);
              this.$modal.msgSuccess("修改成功");
              this.limit = false;
              this.op = false;
              this.getList();
            });
          }

        }
      })
    },
    handleSmoke(){
      this.$confirm('此操作将触发烟雾报警器, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          getSmokeAPI().then(res=>{
            console.log(res);
            if(res.code===200){
              this.$message({
            type: 'success',
            message: '触发成功!'
          });
            }else{
              this.$message({
            type: 'info',
            message: '触发失败'
          });    
            }
          })
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });

    },
    ce(){
      getSmokeAPI().then(res=>{
            console.log(res);
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.el-form-item {
  margin-left: 30px;
}
</style>
